Measuring elapsed time

Problem

You want to measure how much time it takes to run a particular block of code.

Solution

The system.time() function will measure how long it takes to run something in R.

cat("Running XYZ... \n")

system.time({
    # Do something that takes time
    x <- 1:100000
    for (i in seq_along(x))  x[i] <- x[i]+1
})
#   user  system elapsed 
#  0.265   0.001   0.266 

The output means it took 0.266 seconds to run the block of code.